#include "widget.h"
#include "ui_widget.h"
#include "QPainter"
//#include "QPen"
#include "QPicture"
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    connect(ui->Btn1,&QPushButton::clicked,[=](){
        //如果手动调用绘图时间，用update更新
        update();
    });
    QPicture pic;
    QPainter paint1;
    paint1.begin(&pic);//开始
    paint1.setPen(Qt::cyan);
    paint1.drawEllipse(QPoint(100,100),50,50);
    paint1.end();//结束
    //设置保存地方
     pic.save("E:/pic.zt");
}
void Widget::paintEvent(QPaintEvent*){
    //实例化一个画家对象
    //    QPainter *painter=new QPainter(this);
    //    painter->drawLine(QPoint(0,0),QPoint(100,100));
    //这种对象可以这么声明，而且不是指针对象不用分配地址
    QPainter painter(this);
    //设置一个画笔,RGB,这是对象
    QPen pen=(QColor(255,0,0));
    //设置宽度
    pen.setWidth(4);
    //设置画笔风格
    pen.setStyle(Qt::DotLine);
    //添加这个笔给画家
    painter.setPen(pen);

    //添加画刷，用来填充的
    QBrush brush(Qt::cyan);
    //设置风格
    brush.setStyle(Qt::Dense7Pattern);
    //添加
    painter.setBrush(brush);


    //画线
    painter.drawLine(QPoint(0,0),QPoint(100,100));
    //画圆
    painter.drawEllipse(QPoint(100,100),50,80);
    //画矩形
    painter.drawRect(QRect(30,30,60,60));
    //画文字
    painter.drawText(QRect(300,300,400,400),"认真学习");

    //绘图高级设置
    QPainter painter1(this);
     painter1.drawEllipse(QPoint(400,400),50,50);
     //设置抗锯齿能力
     painter1.setRenderHint(QPainter::Antialiasing);
     painter1.drawEllipse(QPoint(500,400),50,50);

      painter1.drawRect(QRect(200,200,50,50));
      //画家移动
      painter1.translate(100,0);
      //保存画家状态
      painter1.save();
      painter1.drawRect(QRect(200,200,50,50));
      painter1.translate(100,0);
      //还原画家状态
      painter1.restore();
      painter1.drawRect(QRect(200,200,50,50));

       posx+=20;
       if(posx>this->width()){
           posx=0;
       }
      painter1.drawPixmap(posx,100,QPixmap(":/Image/12.png"));

      //绘图设备QPixmap
      //创建并设置大小
      QPixmap pix(300,300);
      //设置该设备背景
      pix.fill(Qt::white);
      QPainter painter2(&pix);
      painter2.setPen(Qt::green);
      painter2.drawEllipse(QPoint(100,100),50,50);
      //设置保存地方
      pix.save("E:/pix.png");


      //绘图设备QImage，可以对图片像素点进行访问，创建设置大小和色度
     QImage img(300,300,QImage::Format_RGB32);
     img.fill(Qt::white);
      QPainter painter3(&img);
      painter3.setPen(Qt::blue);
      painter3.drawEllipse(QPoint(100,100),50,50);
      //画完要设置保存地方
      img.save("E:/img.png");


      QImage img2;
      //加载图片
      img2.load(":/Image/780.jpg");

      //对像素进行修改
      for(int i=0;i<100;i++){
          for(int j=0;j<100;j++){
              QRgb value=qRgb(0,255,0);
              img2.setPixel(i,j,value);
          }
      }
       painter1.drawImage(1,1,img2);


       //QPicture指令重现
       QPicture pic;
       pic.load("E:/pic.zt");
       painter1.drawPicture(100,400,pic);


}

Widget::~Widget()
{
    delete ui;
}

